from HomVec3 import HomVec3

class Matrix(object):
  
  def __init__(self,params):
    
    self.matrix=self.createMatrix()
    self.addToMatrix(params)
   
  def __add__(self,sec):
    
    m = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
    for i in range(4):
      for j in range(4):
	m[i][j]=self.matrix[i][j] + sec.matrix[i][j]
    res = m[0]+m[1]+m[2]+m[3]

    return Matrix(res)
    
  def __mul__(self,sec):
    
    if type(sec) == HomVec3:
      res = [0,0,0]
      
      res[0]+=sec.x*self.matrix[0][0] + sec.y*self.matrix[0][1]+sec.z*self.matrix[0][2] + sec.w * self.matrix[0][3]
      
      
      res[1]+=sec.x*self.matrix[1][0] + sec.y*self.matrix[1][1]+sec.z*self.matrix[1][2] + sec.w * self.matrix[1][3]
      
      
      res[2]+=sec.x*self.matrix[2][0] + sec.y*self.matrix[2][1]+sec.z*self.matrix[2][2] + sec.w * self.matrix[2][3]
      

      return HomVec3(res[0],res[1],res[2],sec.w)
    
    elif type(sec) == float or type(sec) == int :
      m = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
      for i in range(4):
	for j in range(4):
	  m[i][j] = self.matrix[i][j]*sec
   
    else:
      m = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
      for i in range(4):
	for j in range(4):
	  for k in range(4):
	    m[i][j] += self.matrix[i][k]*sec.matrix[k][j]
    res = m[0]+m[1]+m[2]+m[3]

    return Matrix(res)
      
  def addToMatrix(self,params):
    
    i,j=0,0
    for ele in params:
      self.matrix[j][i]=ele
      i+=1
      if i%4 == 0:
	j+=1
	i=0
    
  def __repr__(self):
    return "\n"+str(self.matrix[0])+"\n"+str(self.matrix[1])+"\n"+str(self.matrix[2])+"\n"+str(self.matrix[3])+"\n"
  
  def createMatrix(self):
    
    matrix = [[1,0,0,0],
	      [0,1,0,0],
	      [0,0,1,0],
	      [0,0,0,1]]
    return matrix
    
if __name__=="__main__":
  """
  m1 = Matrix((1,2,2,5,7,3,4,9,3))
  print m1
  m2= Matrix((5,6,7,8))
  v1 = HomVec3(3,2,3,1)
  
  print m1 * v1
  
  
  print m1
  print "\n"
  print m2
  print "\n"
  print m1 * m2 
  print "\n"
  print m1 * 3
  print "\n"
  print m1 + m2
  """
 
